-module(tcp_listener).

-behaviour(gen_server).

-export([start_link/2]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-define(TCP_OPTIONS, [binary, 
					  {packet, 0}, 
					  {active, false}, 
					  {reuseaddr, true}, 
					  {nodelay, false}, 
					  {delay_send, true}, 
					  {send_timeout, 5000}, 
					  {keepalive, true}, 
					  {exit_on_close, true}]).

%%----------------------------------------------------------------------------

start_link(AcceptorCount, Port) ->
    gen_server:start_link(?MODULE, {AcceptorCount, Port}, []).

%%----------------------------------------------------------------------------

%% 监听端口
init({AcceptorCount, Port}) ->
	process_flag(trap_exit, true),
	case gen_tcp:listen(Port, ?TCP_OPTIONS) of
		 {ok, LSock} ->
			             lists:foreach(fun (_) ->
                                  {ok, _APid} = supervisor:start_child(
                                                  tcp_acceptor_sup, [LSock])
                          end,
                          lists:duplicate(AcceptorCount, dummy)),
			 {ok, LSock};
		{error, Reason} ->
			{stop, {cannot_listen, Reason}}
	end.

handle_call(_Request, _From, State) ->
    {reply, State, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info(_Info, State) ->
    {noreply, State}.

terminate(_Reason, State) ->
	%% Returns the local address and port number for a socket
    {ok, {IPAddress, Port}} = inet:sockname(State),
	io:format("listening stopped ip : ~p, port : ~p ~n", [IPAddress, Port]),
    gen_tcp:close(State),
	ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.